package com.zhongketech.sunflower.receiver;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.database.Data;
import com.zhongketech.sunflower.database.PhoneNumberData;
import com.zhongketech.sunflower.filter.PhoneManager;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Utilities;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class SmsReceiver extends BroadcastReceiver{

	private static final String TAG = "SmsReceiver";

	private static boolean incomingFlag = false;

	private static String incoming_number = null;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		Utilities.logCat(TAG, "Action :"+ intent.getAction(),Constants.LOG_ONLYSHELL);
		//如果是收到短信
		if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
		{               

			String number = "";// 短信发件人  
			StringBuilder body = new StringBuilder();// 短信内容


			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				Object[] objArray = (Object[]) bundle.get("pdus");
				SmsMessage[] messages = new SmsMessage[objArray.length];
				Utilities.logCat(TAG, "objArray :"+ objArray,Constants.LOG_ONLYSHELL);
				Utilities.logCat(TAG, "messages :"+ messages,Constants.LOG_ONLYSHELL);
				for (int i = 0; i < objArray.length; i++)
				{
					Utilities.logCat(TAG, "messages[i] :"+ messages[i],Constants.LOG_ONLYSHELL);
					messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);
				}


				for (SmsMessage currentMessage : messages)  
				{
					Utilities.logCat(TAG, "currentMessage :"+currentMessage,Constants.LOG_ONLYSHELL);
					number= currentMessage.getDisplayOriginatingAddress();
					body.append(currentMessage.getDisplayMessageBody());     
				}    

				Utilities.logCat(TAG, "number :"+ number,Constants.LOG_ONLYSHELL);
				Utilities.logCat(TAG, "body :"+ body,Constants.LOG_ONLYSHELL);


				String smsNumber = Utilities.replace(number.toString());
				if (smsNumber.contains("+86")) {  
					smsNumber = smsNumber.substring(3);  
				}  

				Utilities.logCat(TAG, "smsNumber :"+ smsNumber,Constants.LOG_ONLYSHELL);

				// 第二步:确认该短信内容是否满足过滤条件  
				boolean flags_filter = false;  
				for(int i = 0;i < Data.getinstance().getDataCount(Data.DATATYPE_PHONEBLACKLIST);i ++)
				{

					PhoneNumberData num = (PhoneNumberData)Data.getinstance().getData(i,Data.DATATYPE_PHONEBLACKLIST);
					Utilities.logCat(TAG, "Data.getinstance().getPhoneNumberDataByIndex(i).getNumber() :"+ num.getNumber(),Constants.LOG_ONLYSHELL);
					if (smsNumber.equals(num.getNumber())) {
						flags_filter = true;  
						Toast.makeText(context.getApplicationContext(), "被禁止的短信，来自:"+smsNumber,
								Toast.LENGTH_SHORT).show();

					}  
					// 第三步:取消  
					if (flags_filter) {  
						this.abortBroadcast();  
						break;
					} 
				}	
			}  





		}
	}


}
